<?php
/* /app/View/Helper/NavigationHelper.php */
App::uses('AppHelper', 'View/Helper');

class NavigationHelper extends AppHelper {
	public $helpers = array('Html');

	function beforeRender() {
		$this->Html->css(array('superfish'), 'stylesheet', array('inline' => false, 'once'=>true ))."\n";
		$this->Html->script(array('superfish'), array('inline' => false, 'once'=>true ));
	}

	function buildMenu($menuTree){
		return $this->Html->tag('ul', $this->getListItems($menuTree), array('class'=>'sf-menu light-background')).
			$this->Html->scriptBlock('$(function(){$("ul.sf-menu").superfish()})');

	}

	private function getListItems($menuTree){
		$listItems = '';
		foreach($menuTree as $menuItem){
			$listItems .= $this->Html->tag('li',
				(array_key_exists('url', $menuItem)?
					$this->Html->link($menuItem['name'], $menuItem['url']):
					$menuItem['name']
				).
				(array_key_exists('children', $menuItem)?
					$this->Html->tag('ul',$this->getListItems($menuItem['children'])):
					''
				).PHP_EOL
			);
		}
		return $listItems;
	}
}
